package com.platform.web.controller.base;

import java.util.List;

import com.platform.base.domain.BaseFactory;
import com.platform.base.service.IBaseFactoryService;
import com.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.base.domain.MeterType;
import com.platform.base.service.IMeterTypeService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 热型号Controller
 * 
 * @author wangqi
 * @date 2020-09-26
 */
@Controller
@RequestMapping("/base/meter/type")
public class MeterTypeController extends BaseController
{
    private String prefix = "base/meter/type";

    @Autowired
    private IMeterTypeService meterTypeService;

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @RequiresPermissions("base:meter:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询热型号列表
     */
    @RequiresPermissions("base:meter:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MeterType meterType)
    {
        startPage();
        List<MeterType> list = meterTypeService.selectMeterTypeList(meterType);
        return getDataTable(list);
    }

    /**
     * 新增热型号
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/add";
    }

    /**
     * 新增保存热型号
     */
    @RequiresPermissions("base:meter:type:add")
    @Log(title = "热型号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MeterType meterType)
    {
        meterType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(meterTypeService.insertMeterType(meterType));
    }

    /**
     * 修改热型号
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        MeterType meterType = meterTypeService.getById(typeId);
        mmap.put("meterType", meterType);
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/edit";
    }

    /**
     * 修改保存热型号
     */
    @RequiresPermissions("base:meter:type:edit")
    @Log(title = "热型号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MeterType meterType)
    {
        meterType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(meterTypeService.updateMeterType(meterType));
    }

    /**
     * 删除热型号
     */
    @RequiresPermissions("base:meter:type:remove")
    @Log(title = "热型号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(meterTypeService.deleteMeterTypeByIds(ids));
    }
}
